/**
 * 
 */
package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.PrivateDirectInvestMentFund;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

/**
 * 类说明
 * @author HUNING
 * @since 2018-1-4
 * 
 */
public class PrivateDirectInvestMentDetailProcessor implements PageProcessor{

	
	private static Logger logger = Logger.getLogger(PrivateDirectInvestMentDetailProcessor.class);
	
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
//		System.out.println(page.getContent());
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
//		JsonObject jsonObj1 = new JsonParser().parse(page.getContent()).getAsJsonObject();
//		
//		JSONArray jsonArr = JSONArray.fromObject(page.getContent());
//		JSONObject jsonObj = jsonArr.getJSONObject(0);
		
		
		
		
		int count = atomicInteger.incrementAndGet();
		logger.info("正在解析第"+count+"条数据:");
		
		
		PrivateDirectInvestMentFund fundInfo = new PrivateDirectInvestMentFund(); 
		fundInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		fundInfo.setFundName(jsonObj.getString("cpmc"));//产品名
		fundInfo.setFundNo(jsonObj.getString("cpbm"));//产品编码
		fundInfo.setManagerName(jsonObj.getString("gljg"));//管理机构
		
		fundInfo.setScale(jsonObj.getString("clgm"));//成立规模
		fundInfo.setHouseHolds(jsonObj.getInt("clscyhs"));//成立时参与户数
		fundInfo.setEndDate(jsonObj.getString("dqr"));//到期日
		fundInfo.setManageType(jsonObj.getString("glfs"));//管理方式
		fundInfo.setRegiOrg(jsonObj.getString("fedjjg"));//份额登记机构
		fundInfo.setGrade(jsonObj.getString("sffj"));//是否分级	
		fundInfo.setCustodian(jsonObj.getString("tgjg"));//托管机构	
		fundInfo.setInvestType(jsonObj.getString("tzlx"));//投资类型
		fundInfo.setEstablishDate(DateUtils.parseDate(jsonObj.getString("slrq")));//设立日期
		fundInfo.setId(jsonObj.getString("id"));
		fundInfo.setUrl("http://gs.amac.org.cn/amac-infodisc/res/pof/securities/detail.html?id="+jsonObj.getString("id"));
		StringUtils.removeSpace(fundInfo);
		objectContainer.add(fundInfo);
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
